// pages/user/login.js
const App = getApp()
import Util from '../../utils/util'

Page({

  data: {
    init: false, // 界面是否初始化中
    loading: false, // 是否loading状态
    users: [],
    user: {}
  },
  // 页面加载完成
  onLoad() {
    this.waitForReady()
  },
  waitForReady() {
    if (App.Global.OpenId) {

      this.execLoad()
    } else {
      setTimeout(() => {
        this.waitForReady()
      }, 200);
    }
  },
  // 加载店员
  execLoad() {
    App.doPost({
      uri: '/app/user/account/wxmn/clerk/list',
      body: {
        appid: App.Global.AppId,
        openid: App.Global.OpenId,
      },
      fnSucc: resp => {
        this.setData({
          user: App.Global.User,
          users: resp.data.users,
          init: true
        })
      },
      fnFail: resp => {
        Util.Notify.error(`[${resp.stat.err}] ${resp.stat.msg}`)
      },
      fnComp: () => {
        this.setData({
          loading: false
        })
      }
    })
  },
  // 选择用户
  doSelectNowUser(e) {
    let user = e.currentTarget.dataset.user
    Util.Dialog.confirm(`是否以 [ ${user.name} ] 身份使用小程序?`, () => {
      wx.setStorageSync('App.Last.Uid', user.id)
      wx.setStorageSync('App.Uid', user.id)
      wx.setStorageSync('App.Token', user.token)
      wx.setStorageSync('App.Name', user.name)
      wx.setStorageSync('App.User', JSON.stringify(user))
      App.Global.User = user
      wx.reLaunch({
        url: '/pages/index/index'
      })
    })
  }
})